package com.lebang.controller.ybdapi;

import com.alibaba.fastjson.JSONObject;
import com.lebang.domain.ybd.MemberGoldConfig;
import com.lebang.domain.ybd.MemberGoldRecord;
import com.lebang.entity.RequestHeaders;
import com.lebang.feign.PointEarnedFeign;
import com.lebang.model.RequestUserInfo;
import com.lebang.model.back.Result;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.LuckDrawService;
import com.lebang.service.MemberNewTaskService;
import com.lebang.service.MemberNoticeService;
import com.lebang.service.PointService;
import com.lebang.service.ybd.MemberGoldConfigService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: hwq
 * @Date: 2020/3/24 20:07
 * @Version 1.0
 */
@Api("金币赚")
@RestController
@RequestMapping({"/ybd/point"})
public class PointEarnedController {
    @Autowired
    private MemberGoldConfigService memberGoldConfigService;

    @Autowired
    private PointService pointService;
    @Autowired
    private MemberNoticeService memberNoticeService;
    @Autowired
    private LuckDrawService luckDrawService;
    @Autowired
    private MemberNewTaskService memberNewTaskService;
    @PostMapping("/info")
    public JsonData info(HttpServletRequest request) {
        RequestHeaders headers=new RequestHeaders(request);
        List<MemberGoldConfig> list = memberGoldConfigService.selectAll();
        list = memberGoldConfigService.filter(list, headers);
        return Callback.success("success", "查询成功", list);
    }


    @PostMapping("/pull")
    public JsonData pull(HttpServletRequest request, @RequestParam("goldId") Integer goldId) {
        RequestHeaders headers=new RequestHeaders(request);
        MemberGoldConfig config = memberGoldConfigService.selectById(goldId);
        if (config == null) {
            return Callback.fail("200", "配置不存在");
           // return new Response<Integer>().fail("success", "配置不存在");
        }
        if (memberGoldConfigService.getCount(headers,config ) >= config.getMaxReceiveCount()) {
            return Callback.fail("201", "该金币今日已达上限");
            //return new Response<Integer>().fail("success", "金币冷却中,可以看视频加快速度哦~");
        }
        if (memberGoldConfigService.checkCd(config, headers) > 0) {
            return Callback.fail("202", "金币冷却中,可以看视频加快速度哦~");
            //return new Response<Integer>().fail("success", "金币冷却中,可以看视频加快速度哦~");
        }
        MemberGoldRecord record= memberGoldConfigService.pull(config,headers);
        pointService.addPoint(headers.getUserid(),record.getPoint(),TaskCategory.POINT_EARN,headers,"金币奖励");
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("id",record.getId());
        jsonObject.put("point",record.getPoint());
        return Callback.success("200", "领取成功!",jsonObject);
       // return new Response<Integer>().fail("success", "领取成功~",config.getPoint());
    }

    @PostMapping("/double")
    public JsonData pullDouble(HttpServletRequest request, @RequestParam("recordId") Long recordId) {
        RequestHeaders headers=new RequestHeaders(request);
        MemberGoldRecord record = memberGoldConfigService.selectRecordById(recordId);
        if (record == null) {
            return Callback.fail("200", "记录不存在");
        }
        if (record.getIsDouble().equals(1)) {
            return Callback.fail("200", "已经翻倍了");
        }
        int point =record.getPoint();
        memberGoldConfigService.pullDouble(record);
        pointService.addPoint(headers.getUserid(),point,
                TaskCategory.WALK_EARNS,headers,"翻倍奖励:"+point+"金币");
        memberNoticeService.addNoticeAndPush("金币赚","金币赚翻倍奖励:"+point+"金币",headers.getUserid(),headers);
        return Callback.success("200", "翻倍成功~");

    }


    @PostMapping("/cool")
    public JsonData cool(HttpServletRequest request, @RequestParam("goldId") Integer goldId) {
        RequestHeaders headers=new RequestHeaders(request);
        MemberGoldConfig config = memberGoldConfigService.selectById(goldId);
        if (config == null) {
            return Callback.fail("200", "配置不存在");
            // return new Response<Integer>().fail("success", "配置不存在");
        }
        memberGoldConfigService.subTime(config,headers,120);
        return Callback.success("success", "成功~");
    }
}
